// pages/modificationAddress/modificationAddress.js
var utils = require('../../utils/util.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['重庆市', '重庆市', '沙坪坝区'],
    isChecked:false
  },

  /**
   * 选择地址
   */
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },

  /**
   * 设置为默认按钮
   */
  checkChange:function () {
    var check = {}
    check['isChecked'] = this.data.isChecked ? false : true
    this.setData(check)
  },

  /**
   * 保存按钮
   */
  tapSave: function (e) {
    var _this = this
    var province = e.detail.value.region[0]
    var city = e.detail.value.region[1]
    var area = e.detail.value.region[2]
    var is_default = 0
    if (_this.data.isChecked) {
      is_default = 1
    }
    wx.request({
      url: app.globalData.requestip + "&c=Address&a=addAddress",
      data: {
        name: e.detail.value.name,
        phone: e.detail.value.phone,
        province:province,
        city:city,
        area:area,
        address: e.detail.value.address,
        is_default:is_default
      },
      header: app.globalData.header,
      success: function (res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: '保存成功',
            duration: 2000,
            success: function (res) {
              wx.navigateBack({
                delta: 1,
              })
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        } else {
          utils.loadFail(res)
        }
      },
      fail: function (res) {
        utils.loadRequestFail(res)
      }
    })
  },

  /**
   * 取消按钮
   */
  tapCancel:function () {
    wx.navigateBack({
      delta: 1,
    })
  }
})

